<?php

namespace app\store\model\appointment;

use app\common\model\BaseModel;
use think\Db;


/**
 * 预约配置模型
 * Class PointsLog
 * @package app\store\model\user
 */
class Config extends BaseModel
{
    protected $name = 'appointment_config';

    /**
     *  预约时间
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getList()
    {
        // 获取列表数据
        return $this->where('is_delete', 0)
            ->where('wxapp_id', self::$wxapp_id)
            ->field('time_id,name')->order('create_time desc')->select();
    }

    /**
     * 预约类型
     * @param array $query
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getListByPage()
    {
        // 获取列表数据
        return $this->alias('config')
            ->field('config_id,
            config.time_id,type.name type_name,config.create_time,config.date,config.service_id,config.count')
//            ->join('appointment_time time', 'config.time_id = time.time_id')
            ->join('appointment_type type', 'config.type_id = type.type_id')
            ->where('config.is_delete', 0)
            ->order(['config_id' => 'desc'])
            ->paginate(15, false, [
                'query' => \request()->request()
            ])->each(function ($item, $key) {
                $item['date_name'] = date("Y-m-d", $item['date']);

                $users = Db("user_address")->where('user_id', 'in', explode(',', $item['service_id']))->column('name');
                $item['service_name'] = isset($users) ? implode(',', $users) : '';
                $time_id = explode(',',$item['time_id']);
                $time_list = Db::name('appointment_time')->where(['time_id'=>['in',$time_id]])->column('name');
                $str = '';
                foreach ($time_list as $k => $val){
                    if($k == 0){
                        $str .= $val;
                    }else{
                        $str .= '<br>'.$val;
                    }
                }
                $item['time_name'] = $str;
            });
    }

    /**
     * 新增记录
     * @param $data
     * @return bool
     * @throws \Exception
     */
    public function add($data)
    {
        if (empty($data['date'])) {
            $this->error = '预约日期未选择';
            return false;
        }

        $data['date'] = strtotime($data['date']);

        if (empty($data['time_id'])) {
            $this->error = '时间点未选择';
            return false;
        }
        $data['time_id'] = implode(',',$data['time_id']);
        if ($data['count'] < 1) {
            $this->error = '服务人员人数不能为0';
            return false;
        }


        if (empty($data['type_id'])) {
            $this->error = '预约类型未选择';
            return false;
        }

        $data['wxapp_id'] = self::$wxapp_id;
        return $this->allowField(true)->save($data);
    }

    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }

    /**
     * 类型详情
     * @param $config_id
     * @param array $with
     * @return static|null
     * @throws \think\exception\DbException
     */
    public static function detail($config_id, $with = [])
    {
        return static::get($config_id, $with);
    }

}